<template>
  <div class="app-container">
    <t-form :data="queryParams" ref="queryRef" layout="inline" class="mb20">
      <t-form-item label="登录地址" name="ipaddr">
        <t-input
          v-model="queryParams.ipaddr"
          placeholder="请输入登录地址"
          clearable
          style="width: 200px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item label="用户名称" name="userName">
        <t-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          style="width: 200px"
          @enter="handleQuery"
        />
      </t-form-item>
      <t-form-item>
        <t-space>
          <t-button theme="primary" @click="handleQuery">
            搜索
            <template #icon>
              <t-icon name="search" />
            </template>
          </t-button>
          <t-button theme="default" @click="resetQuery">
            重置
            <template #icon>
              <t-icon name="refresh" />
            </template>
          </t-button>
        </t-space>
      </t-form-item>
    </t-form>
    <t-table
      v-loading="loading"
      :data="onlineList.slice((pageNum - 1) * pageSize, pageNum * pageSize)"
      :columns="columns"
      row-key="tokenId"
      style="width: 100%"
    >
      <template #serial="scope">
        <span>{{ (pageNum - 1) * pageSize + scope.rowIndex + 1 }}</span>
      </template>
      <template #loginTime="scope">
        <span>{{ parseTime(scope.row.loginTime) }}</span>
      </template>
      <template #action="scope">
        <t-button
          variant="text"
          type="primary"
          @click="handleForceLogout(scope.row)"
          v-hasPermi="['monitor:online:forceLogout']"
          >强退
          <template #icon>
            <t-icon name="logout" />
          </template>
        </t-button>
      </template>
    </t-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="pageNum"
      v-model:limit="pageSize"
    />
  </div>
</template>

<script setup>
import { forceLogout, list as initData } from "@/api/monitor/online";
import { getCurrentInstance, ref } from "vue";

const { proxy } = getCurrentInstance();

const onlineList = ref([]);
const loading = ref(true);
const total = ref(0);
const pageNum = ref(1);
const pageSize = ref(10);

const columns = ref([
  { colKey: "serial", title: "序号", width: 60, align: "center" },
  { colKey: "tokenId", title: "会话编号", align: "center", ellipsis: true },
  {
    colKey: "userName",
    title: "登录名称",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "deptName",
    title: "所属部门",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "ipaddr",
    title: "主机",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "loginLocation",
    title: "登录地点",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "os",
    title: "操作系统",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "browser",
    title: "浏览器",
    align: "center",
    ellipsis: true,
  },
  {
    colKey: "loginTime",
    title: "登录时间",
    align: "center",
    width: 180,
    ellipsis: true,
  },
  {
    colKey: "action",
    title: "操作",
    align: "center",
    width: 180,
  },
]);

const queryParams = ref({
  ipaddr: undefined,
  userName: undefined,
});

/** 查询登录日志列表 */
function getList() {
  loading.value = true;
  initData(queryParams.value).then((response) => {
    onlineList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  pageNum.value = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 强退按钮操作 */
function handleForceLogout(row) {
  proxy.$modal
    .confirm('是否确认强退名称为"' + row.userName + '"的用户?')
    .then(function () {
      return forceLogout(row.tokenId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
